package track2.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;

import track2.context.Config;

public class SampleData {
	
	public static void sampleData(String outputTestPath, //Test output path
			String outputSolutionPath, //Test solution file path
			int sampleSize){ //sample size
		
		System.out.println("begin");
		HashSet<Integer> sampleSet = getSampleSet(sampleSize);
		
		System.out.println("enter");
		try{
			BufferedReader br = new BufferedReader(new FileReader(Config.trainingFilePath));
			BufferedWriter outputContent = new BufferedWriter(new FileWriter(outputTestPath));
			BufferedWriter outputSolution = new BufferedWriter(new FileWriter(outputSolutionPath));
			BufferedWriter outputTrain = new BufferedWriter(new FileWriter(Config.sampleTrainFilePath));
			
			int index = 0;
			String line;
			while ((line = br.readLine()) != null){
				if (sampleSet.contains(index)){
					String[] paras = line.split("\t");// id seg1|seg2|seg3....
					
					double click = Double.parseDouble(paras[0]);
					double impression = Double.parseDouble(paras[1]);
					double p = click/impression;
					outputSolution.write(String.valueOf(p));
					outputSolution.write(",");
					outputSolution.write(String.valueOf((int)impression));
					outputSolution.write("\n");
					
					outputContent.write(line.substring(line.indexOf("\t", line.indexOf("\t")+1)+1));
					outputContent.write("\n");
				}
				else
				{
					outputTrain.write(line);
					outputTrain.write("\n");
				}
				index++;
				if (index % 1000000 == 0)
					System.out.println(index);
			}
			
			br.close();
			outputContent.close();
			outputSolution.close();
			outputTrain.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static HashSet<Integer> getSampleSet(int sampleSize){
		HashSet<Integer> sampleSet = new HashSet<Integer>();
		int[] indexes = new int[Config.trainInstanceNum];
		for (int i = 0; i < Config.trainInstanceNum; i++)
		{
			if (i % 1000000 == 0)
				System.out.println(i);
			indexes[i] = i;
		}
		
		System.out.println("step");
		
		Random r = new Random();
		int temp;
		for (int i = 0; i < sampleSize; i++){
			if (i % 1000000 == 0)
				System.out.println(i);
			int j = r.nextInt(Config.trainInstanceNum - i) + i;
			temp = indexes[i];
			indexes[i] = indexes[j];
			indexes[j] = temp;
			sampleSet.add(indexes[i]);
		}
		
		return sampleSet;
	}

	public static void main(String[] args){
		sampleData(Config.trainingFolderPath + "testing_sample_content.txt",
				Config.trainingFolderPath + "testing_sample_solution.csv",
				Config.testInstanceNum);
	}
}
